package officeboost.domain;

import java.text.SimpleDateFormat;
import java.util.*;
import javassist.bytecode.Descriptor;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import officeboost.domain.equip_venue.Equipment;
import officeboost.domain.equip_venue.Reservation;
import officeboost.domain.equip_venue.Venue;

@Stateless
public class VenueService {

    @PersistenceContext
    EntityManager em;
    @EJB
    LogMgr logger;

    public Venue find(String id) {
        return em.find(Venue.class, id);
    }

    public void persist(Venue venue) {
        if (venue == null) {
            return;
        }
        if (find(venue.getId()) != null) {
            logger.log("venue:" + venue.getId() + "主键重复！");
            return;
        }
        em.persist(venue);
    }

    // 按id删除
    public void deleteById(String id) {
        Venue venue = find(id);
        if (venue == null) {
            return;
        }
        delete(venue);
    }

    // delete an venue
    public void delete(Venue venue) {
        em.remove(venue);
    }

    public List<Venue> findAll() {
        Query query = em.createQuery("from Venue e");
        List<Venue> venues = query.getResultList();
        if (venues == null) {
            venues = new ArrayList<>();
        }
        return venues;
    }

    public boolean reserve(Venue c, GregorianCalendar startTime, GregorianCalendar endTime) {
        //boolean result = c.reserve(startTime, endTime);
        //return result;
        return false;
    }

    public boolean cancelReserve(Venue c, int id) {
        //boolean isCanceled = c.cancelReserve(id);
        //return isCanceled;
        return false;
    }

    public boolean checkIfCanBeBooked(String user, String id, Date beginDate, Date endDate) {
        boolean result = false;
        Venue venue = find(id);
        if (venue == null) {
            return false;
        }
        List<Reservation> list = venue.getReservations();
        if (list == null) {
            System.out.println("list ==null");
            list = new ArrayList<Reservation>();
//            logger.log("list.size() == 0");
            System.out.println("list.size() == 0");
            result = true;
            Reservation reservation = new Reservation();
            reservation.setUser(user);
            reservation.setBegin(beginDate);
            reservation.setEnd(endDate);
            list.add(reservation);
            venue.setReservations(list);
//            System.out.println("list.size() = " + venue.getReservations().size());
            em.persist(venue);
//            em.persist(reservation);

        } else {
            System.out.println("list.size() != 0");
            boolean canAdd = true;
            Iterator it = list.iterator();
            while (it.hasNext()) {
                Reservation r = (Reservation) it.next();
                Date tempB = r.getBegin();
                Date tempE = r.getEnd();

                if ((compare(endDate, tempB) != -1) || (compare(beginDate, tempE) != 1)) {
                } else {
                    canAdd = false;
                }
            }
            if (canAdd) {
                result = true;
                Reservation reservation = new Reservation();
                reservation.setUser(user);
                reservation.setBegin(beginDate);
                reservation.setEnd(endDate);
                list.add(reservation);
                venue.setReservations(list);
                em.persist(venue);
//                em.persist(reservation);
            }
        }
        return result;
    }

    public void canNotBeUsed(String id) {
        Venue venue = find(id);
        if (venue == null) {
            return;
        }
        venue.setState("occupied");
        em.merge(venue);
    }

    public void canBeUsed(String id) {
        Venue venue = find(id);
        if (venue == null) {
            return;
        }
        venue.setState("availiable");
        em.merge(venue);
    }

    public String resultReservationList(String id) {
        SimpleDateFormat FORMAT = new SimpleDateFormat(
                "yy/MM/dd HH:mm");

        Venue venue = find(id);
        if (venue == null) {
            return null;
        }
        List<Reservation> list = venue.getReservations();
        if (list == null) {
            list = new ArrayList<Reservation>();
            return null;
        }
        Collections.sort(list);
        Iterator it = list.iterator();
        String result = "";
        while (it.hasNext()) {
            Reservation r = (Reservation) it.next();
            String str1 = FORMAT.format(r.getBegin());
            String str2 = FORMAT.format(r.getEnd());
            if(result.equals("")){
                result = result + "开始：" + str1 + "终止：" + str2; 
            }else{
                result = result + "；" + "开始：" + str1 + "终止：" + str2; 
            }
            
        }

        return result;
    }

    public int compare(Date d1, Date d2) {
        SimpleDateFormat FORMAT = new SimpleDateFormat(
                "yy/MM/dd HH:mm");
        String str1 = FORMAT.format(d1);
        System.out.println("str1: " + str1);
        String str2 = FORMAT.format(d2);
        System.out.println("str2: " + str2);

        int result = str1.compareTo(str2);
        if (result > 0) {
            System.out.println(str1 + " 晚于 " + str2);
            return -1;
        } else if (result == 0) {
            System.out.println(str1 + " 等于 " + str2);
            return 0;
        } else {
            System.out.println(str1 + " 早于 " + str2);
            return 1;
        }
    }
}
